/**
 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 鐗堟潈鎵�湁(C)锛氭睙鑻忔绱㈣蒋浠舵湁闄愬叕鍙�
 * <p>
 * 鍏徃鍚嶇О锛氭睙鑻忔绱㈣蒋浠舵湁闄愬叕鍙�
 * <p>
 * 鍏徃鍦板潃锛氫腑鍥借嫃宸炵鎶�煄闈掑北璺�鍙�
 * <p>
 * 缃戝潃: http://www.oceansoft.com.cn
 * <p>
 * 浣滆�: 090922(闄堜紵)
 * <p>
 * 鏂囦欢鍚� com.oceansoft.mobile.common.base.entity.Query.java 
 * <p>
 * 绫讳骇鐢熸椂闂� 2014/3/31 22:29
 * <p>
 * 璐熻矗浜� 090922(闄堜紵)
 * <p>
 * Email:javacspring@gmail.com
 * <p>
 * 鎵�湪缁�: 鎺屼笂鍏畨搴旂敤骞冲彴
 * <p>
 * 鎵�湪閮ㄩ棬: 寮�彂閮�-鎵嬫寔鎶�湳閮�
 * <p>
 * <p>
 */
package com.oceansoft.mobile.common.base.entity;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.util.StringUtils;

import com.oceansoft.mobile.common.Constant;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: chenw
 * @time: 2014/3/31 22:29
 */
public class Query {

    public static final String ASC = "ASC";
    public static final String DESC = "DESC";

    public static final String PAGE_SQL = "page_sql";
    public static final String COUNT_SQL = "count_sql";

    private int page = 1;
    private int rows = Constant.PAGE_SIZE;
    private String sort;
    private String order = ASC;
    private String query;
    private String qtype;

    public Query() {
    }

    public Query(int page, int rows, String sortname, String sortorder, String query, String qtype) {
        this.page = page;
        this.rows = rows;
        this.sort = sortname;
        this.order = sortorder;
        this.query = query;
        this.qtype = qtype;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public String getSort() {
        return sort;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }

    public String getOrder() {
        return order;
    }

    public void setOrder(String order) {
        this.order = order;
    }

    public String getQuery() {
        return query;
    }

    public void setQuery(String query) {
        this.query = query;
    }

    public String getQtype() {
        return qtype;
    }

    public void setQtype(String qtype) {
        this.qtype = qtype;
    }

    public static String getPageSQL(Query query) {
        return getSQL(null, query).get(PAGE_SQL);
    }

    public static String getPageSQL(String tablPrefix, Query query) {
        return getSQL(tablPrefix, query).get(PAGE_SQL);
    }

    public static String getCountSQL(Query query) {
        return getSQL(null, query).get(COUNT_SQL);
    }

    public static String getCountSQL(String tablPrefix, Query query) {
        return getSQL(tablPrefix, query).get(COUNT_SQL);
    }


    public static Map<String, String> getSQL(String tablPrefix, Query query) {
        final StringBuilder page_sql = new StringBuilder();
        final StringBuilder count_sql = new StringBuilder();
        String tabPre = StringUtils.hasText(tablPrefix) ? (tablPrefix + ".") : tablPrefix;
        //like
        if (StringUtils.hasText(query.getQtype()) && StringUtils.hasText(query.getQuery()) && !"-1".equals(query.getQuery())) {
            page_sql.append(" AND ").append(tabPre).append(query.getQtype().toUpperCase()).append(" LIKE('%").append(query.getQuery()).append("%')");
            count_sql.append(" AND ").append(tabPre).append(query.getQtype().toUpperCase()).append(" LIKE('%").append(query.getQuery()).append("%')");
        }
        //order TODO:注意防范SQL注入
        if (StringUtils.hasText(query.getSort())) {
            page_sql.append(" ORDER BY ").append(tabPre).append(query.getSort().toUpperCase()).append(" ");
            page_sql.append(StringUtils.hasText(query.getOrder()) ? query.getOrder().toUpperCase() : Query.ASC);
        }
        //分页
        if(query.getPage()>0) {
            int _size = 0 != query.getRows() ? query.getRows() : Constant.PAGE_SIZE;
            int _start = 2 > _size ? 0 : _size * (query.getPage() - 1);
            page_sql.append(String.format(" LIMIT %1$d,%2$d", _start, _size));
        }
        return new HashMap<String, String>() {{
            put(PAGE_SQL, page_sql.toString());
            put(COUNT_SQL, count_sql.toString());
        }};
    }
}
